using UnityEditor;
using UnityEngine;

namespace FShaderInspector
{
    /// <summary>
    /// 启用标签
    /// </summary>
    public class EnableDrawer : FMaterialPropertyDrawer
    {
        public EnableDrawer(string keyword) { this.keyword = keyword; }

        public EnableDrawer(string keyword, float level = 1)
        {
            this.keyword = keyword;
            this.level = (int)level;
        }


        public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
        {
            base.OnGUI(position, prop, label, editor);

            // 如果level等级 > ShaderGUI等级 && 不是折叠状态 && 是子集的情况下不执行
            if (level > FShaderGUI.level && !FShaderGUI.isOpen && FShaderGUI.IsChild())
                return;

            var hasMixedValue = HasMixedFoldout();

            // 检测宏是否开启
            if (!IsKeywordEnabled())
                EditorCache.instance.SetKey(labelKey, false);

            // 记录GUI默认颜色
            var guiColor = GUI.color;

            // 设置未开启颜色
            if ((int)prop.floatValue == 0)
                GUI.color = new Color(0.7f, 0.7f, 0.7f, 0.7f);

            // 设置冲突颜色
            if (prop.hasMixedValue)
                GUI.color = new Color(1, 0.4F, 0.4F, 1);

            // 绘制折页按钮
            var btnRect = FShaderGUI.GetTempRect();
            if (GUI.Button(new Rect(btnRect.x, btnRect.y, btnRect.width - 50, btnRect.height), "", EditorStyles.boldLabel))
            {
                if (IsKeywordEnabled() && !prop.hasMixedValue)
                {
                    if (!hasMixedValue)
                    {
                        foreach (var target in prop.targets)
                        {
                            var key = EditorCache.instance.GetPropertyKey(target, prop.name);
                            var state = EditorCache.instance.GetKey(key);
                            EditorCache.instance.SetKey(key, !state);
                        }
                    }
                    else
                    {
                        // 不同值状态时 默认点击打开
                        foreach (var target in prop.targets)
                        {
                            var key = EditorCache.instance.GetPropertyKey(target, prop.name);
                            EditorCache.instance.SetKey(key, true);
                        }
                    }
                }

                // 按住alt按键 展开折叠所有子选项
                if (Event.current.alt)
                    FShaderGUI.SetFoldedChild(prop.name, level, EditorCache.instance.GetKey(labelKey));
            }

            // 绘制开关按钮
            if (GUI.Button(new Rect(btnRect.width - 50, btnRect.y + 1, 50, btnRect.height), GetBtnIcon(prop)))
            {
                if (prop.hasMixedValue)
                {
                    // 不同值状态时 默认点击打开
                    SetPropBoolAsFloat(true);
                    SetFoldoutState(true);
                }
                else
                {
                    // 正常处理
                    if (!GetPropFloatAsBool())
                    {
                        SetPropBoolAsFloat(true);
                        SetFoldoutState(true);
                    }
                    else
                    {
                        SetPropBoolAsFloat(false);
                        SetFoldoutState(false);
                    }
                }
            }

            label = GetFoldoutIcon(false, hasMixedValue);
            label.text = Localization.FindName(prop.displayName);

            switch (level)
            {
                case <= 1:
                    FShaderGUI.DrawFoldoutGUI(btnRect, label, new Color(0.75f, 0.75f, 0.75f, 1), true);
                    break;
                case 2:
                    FShaderGUI.DrawFoldoutGUI(btnRect, label, new Color(0, 0, 0, 0.2f), false);
                    break;
                default:
                    FShaderGUI.DrawFoldoutGUI(btnRect, label, new Color(0, 0, 0, 0), false);
                    break;
            }

            // 恢复GUI默认颜色
            GUI.color = guiColor;

            // 传递当前页面 level
            FShaderGUI.level = level;

            if (prop.hasMixedValue || hasMixedValue)
            {
                // 冲突时设置折页为关闭状态
                FShaderGUI.isOpen = false;
            }
            else
            {
                // 正常时 则在Editor里获取
                FShaderGUI.isOpen = EditorCache.instance.GetKey(labelKey);
            }
        }

        /// <summary> 获取按钮 icon </summary>
        GUIContent GetBtnIcon(MaterialProperty prop)
        {
            var btnIcon = EditorGUIUtility.IconContent((int)prop.floatValue == 1 ? "LockIcon" : "LockIcon-On");
            if (prop.hasMixedValue)
                btnIcon = EditorGUIUtility.IconContent("d_Invalid");

            return btnIcon;
        }
    }
}